<template>
    <div>
        <h1>门店录入</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>账号</td>
                    <td><input type="text" v-model="info.UserName"></td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td><input type="password" v-model="info.Password"></td>
                </tr>
                <tr>
                    <td>门店名称</td>
                    <td><input type="text" v-model="info.RoomName"></td>
                </tr>
                <tr>
                    <td>分店名称</td>
                    <td><input type="text" v-model="info.FDName"></td>
                </tr>
                <tr>
                    <td>门店图片</td>
                    <td><input type="file" @change="Imgs">
                        <img :src="info.Img" width="60" height="60" v-if="info.Img != ''">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td><input type="radio" name="str" :value="true" v-model="info.State">启用
                        <input type="radio" name="str" :value="false" v-model="info.State">禁用
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="录入" @click="ok"></td>
                </tr>

            </tbody>

        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import moment from 'moment';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
let info: any = reactive({
    "UserName": "",
    "Password": "",
    "RoomName": "",
    "FDName": "",
    "Img": "",
    "State": ''
})

const Imgs = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: '/api/Room/Img',
        method: 'post',
        data: f
    })
        .then(res => {
            console.log(res);
            if (res.data == ("上传大于2M")) {
                alert('图片上传大于2M')
                return;
            }
            info.Img = res.data

        })
        .catch(err => {
            console.log(err);

        })
}


const ok = () => {
    if (info.UserName == "") {
        alert('账号不能为空')
        return;
    }
    if (info.Password == "") {
        alert('密码不能为空')
        return;
    }
    if (info.RoomName == "") {
        alert('门店名称不能为空')
        return;
    }
    if (info.FDName == "") {
        alert('分店名称不能为空')
        return;
    }
    if (info.Img == "") {
        alert('图片不能为空')
        return;
    }
    axios({
        url: '/api/Room/Add',
        method: 'post',
        data: info
    })
        .then(res => {
            if (res.data == -1) {
                alert('门店名称重复')
                return;
            }
            if (res.data > 0) {
                alert('录入成功')
                router.push('/')
                return;
            }
            else {
                alert("录入失败")
                return;
            }

        }).catch(err => {
            console.log(err);

        })
}
</script>

<style scoped></style>